package com.leshua.channel.mpos.dto;

import com.alibaba.fastjson.JSON;
import com.leshua.channel.mpos.common.ReturnCode;

/**
 * 公共返回参数
 * 
 * @author Admin
 *
 */
public class CommonRespDto<T> {
	//返回码
	private String returnCode;
	//返回描述
	private String returnDesc;
	//返回数据
	private T data;
	
	public CommonRespDto(){
		
	}
	public CommonRespDto(String returnCode, String returnDesc){
		this.returnCode = returnCode;
		this.returnDesc = returnDesc;
	}
	public CommonRespDto(String returnCode, String returnDesc, T data){
		this.returnCode = returnCode;
		this.returnDesc = returnDesc;
		this.data = data;
	}
	
	
	public String getReturnCode() {
		return returnCode;
	}
	public void setReturnCode(String returnCode) {
		this.returnCode = returnCode;
	}
	public String getReturnDesc() {
		return returnDesc;
	}
	public void setReturnDesc(String returnDesc) {
		this.returnDesc = returnDesc;
	}
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}

	public void setSuccessData(T data){
		this.returnCode = ReturnCode.CODE_0;
		this.returnDesc = ReturnCode.CODE_MAP.get(returnCode);
		this.data =data;
	}

	public void setSuccessData(){
		this.returnCode = ReturnCode.CODE_0;
		this.returnDesc = ReturnCode.CODE_MAP.get(returnCode);
	}

	public void setFailData(T data){
		this.returnCode = ReturnCode.CODE_9;
		this.returnDesc = ReturnCode.CODE_MAP.get(returnCode);
		this.data =data;
	}

	public void setTimeOutData(T data){
		this.returnCode = ReturnCode.CODE_5;
		this.returnDesc = ReturnCode.CODE_MAP.get(returnCode);
		this.data =data;
	}

	public static String returnFail(){
		return new CommonRespDto(ReturnCode.CODE_9, ReturnCode.CODE_MAP.get(ReturnCode.CODE_9)).toString();
	}

	public static String returnSuccess(){
		return new CommonRespDto(ReturnCode.CODE_0, ReturnCode.CODE_MAP.get(ReturnCode.CODE_0)).toString();
	}

	public static String returnTimeOut(){
		return new CommonRespDto(ReturnCode.CODE_5, ReturnCode.CODE_MAP.get(ReturnCode.CODE_5)).toString();
	}

	public static String returnTimeOut(Object data){
		return new CommonRespDto(ReturnCode.CODE_5, ReturnCode.CODE_MAP.get(ReturnCode.CODE_5),data).toString();
	}

	public static String returnFail(Object data){
		return new CommonRespDto(ReturnCode.CODE_9, ReturnCode.CODE_MAP.get(ReturnCode.CODE_9),data).toString();
	}

	public static String returnSuccess(Object data){
		return new CommonRespDto(ReturnCode.CODE_0, ReturnCode.CODE_MAP.get(ReturnCode.CODE_0),data).toString();
	}

	public static String returnResult(String code, String desc, Object data){
		return new CommonRespDto(code,desc,data).toString();
	}
	
	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}
	
	public static CommonRespDto getSuccessResp(Object data){
		CommonRespDto dto = new CommonRespDto();
		dto.setSuccessData(data);
		return dto;
	}

	public static CommonRespDto getSuccessResp(){
		CommonRespDto dto = new CommonRespDto();
		dto.setSuccessData();
		return dto;
	}

	public static CommonRespDto getSuccessResp(Object data,String returnDesc){
		CommonRespDto dto = new CommonRespDto();
		dto.setSuccessData(data);
		dto.setReturnDesc(returnDesc);
		return dto;
	}
	
	public static CommonRespDto getFailResp(Object data){
		CommonRespDto dto = new CommonRespDto();
		dto.setFailData(data);
		return dto;
	}

	public static CommonRespDto getFailMsg(String msg){
		CommonRespDto dto = new CommonRespDto();
		dto.returnCode = ReturnCode.CODE_9;
		dto.returnDesc =msg;
		dto.data=msg;
		return dto;
	}

}
